package com.ruoyi.classes.controller;

import com.ruoyi.classes.domain.Classes;
import com.ruoyi.classes.service.IClassesService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 班级管理Controller
 *
 * @author ruoyi
 * @date 2024-10-17
 */
@RestController
@RequestMapping("/classes/classes")
public class ClassesController extends BaseController {
    @Autowired
    private IClassesService classesService;

    /**
     * 查询班级管理列表
     */
    @PreAuthorize("@ss.hasPermi('sqluser:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(Classes classes) {
        startPage();
        List<Classes> list = classesService.selectClassesList(classes);
        return getDataTable(list);
    }

    /**
     * 导出班级管理列表
     */
    @PreAuthorize("@ss.hasPermi('sqluser:class:export')")
    @Log(title = "班级管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Classes classes) {
        List<Classes> list = classesService.selectClassesList(classes);
        ExcelUtil<Classes> util = new ExcelUtil<Classes>(Classes.class);
        util.exportExcel(response, list, "班级管理数据");
    }

    /**
     * 获取班级管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('sqluser:class:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(classesService.selectClassesById(id));
    }

    /**
     * 新增班级管理
     */
    @PreAuthorize("@ss.hasPermi('sqluser:class:add')")
    @Log(title = "班级管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Classes classes) {
        return toAjax(classesService.insertClasses(classes));
    }

    /**
     * 修改班级管理
     */
    @PreAuthorize("@ss.hasPermi('sqluser:class:edit')")
    @Log(title = "班级管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Classes classes) {
        return toAjax(classesService.updateClasses(classes));
    }

    /**
     * 删除班级管理
     */
    @PreAuthorize("@ss.hasPermi('sqluser:class:remove')")
    @Log(title = "班级管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(classesService.deleteClassesByIds(ids));
    }

    /**
     * 导入文件
     */
    @PreAuthorize("@ss.hasPermi('sqluser:class:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Classes> util = new ExcelUtil<Classes>(Classes.class);
        List<Classes> classList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = classesService.importClass(classList, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Classes> util = new ExcelUtil<Classes>(Classes.class);
        util.importTemplateExcel(response, "班级数据");
    }

    /**
     * 查询年级
     */
    @GetMapping("/grade/list")
    public TableDataInfo gradeList(Classes classes) {
        startPage();
        List<String> gradeList = classesService.selectClassesList(classes).stream().map(Classes::getGrade).distinct().toList();
        return getDataTable(gradeList);
    }
}
